<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/26
 * Time: 10:32
 */

namespace app\api\controller;
use think\Controller;
use think\Db;
import('wxpay.WxPay_Api', EXTEND_PATH);
import('wxpay.WxPay_Notify', EXTEND_PATH);
import('wxpay.WxPay_Sign', EXTEND_PATH);
class Passenger extends Base
{
    //登录  --返回乘客基本信息-侧栏信息
    public function login(){
        $p = input('post.');

        if(request()->isPost()) {

            $where['username'] = $p['username'];
            $where['state'] = '1';  //1-启用
            $user = Db::name('user')->where($where)->find();

            if($p['token']){

                $p['token'] = $user['password'];
                return $this->msg_rtn(1);
            }else {

                if(!$user){

                    return $this->msg_rtn(0,'无此乘客信息/已禁用！');
                }else{

                    if(CorePwd($p['password'],$user['salt']) == $user['password']){

                        $pr_where['user_id'] = $user['user_id'];
                        $info = Db::name('passenger')->where($pr_where)->field('passenger_id,head_portrait AS passenger_head_portrait,name AS passenger_name,phone AS passenger_phone,unit AS passenger_unit')->find();
                        $info['user_id'] = $user['user_id'];
                        $info['is_set_pwd'] = $user['is_set_pwd'];
                        $info['role_id'] = $user['role_id'];
                        $info['token'] = $this->CorePwd($p['password'],$user['salt']);

                        return $this->rtn_data(1,$info);

                    }else{
                        return $this->msg_rtn(0,'密码错误！');
                    }
                }
            }

        }else{
            return $this->msg_rtn(0,'提交方式错误！');
        }
    }

    //首页 -- 新订单
    public function home_page_order(){
        $where['passenger_id'] = input('post.passenger_id');
        $info = Db::name('order')
            ->order('order_time desc')
            ->where($where)
            ->field('order_no,order_time,order_state,departure,destination,booking_type')
            ->limit(5)
            ->select();

        //订单状态，1：待审批，2：已批准，3：已拒绝，4：待用车，5：已派车，6：接驾中，
        //7：已到达出发地，8：已到达目的地，9：待支付，10：待评价，11：已完成，12：已取消，13：已终止，14：无效
        if($info){

            return $this->rtn_data(1,$info);
        }else{
            return $this->msg_rtn(0,'无订单信息！');
        }
    }

    //选择乘车人
    public function change_department_passenger(){
        $p = input('post.');

        $car_passenger_info = Db::name('passenger')->alias('pr')
            ->join('car_use cu','pr.car_use_id = cu.car_use_id')
            ->field('pr.car_use_id,cu.name')
            ->where('pr.passenger_id',$p['passenger_id'])->find();


        $department_info = Db::name('department')->where('car_use_id',$car_passenger_info['car_use_id'])->field('department_id,name')->select();

        foreach ($department_info as $k => $val){
            $department_where['department_id'] = $val['department_id'];
            $passenger_info = Db::name('passenger')->where($department_where)->whereNull('delete_time')->field('passenger_id,name AS passenger_name,phone AS passenger_phone')->select();
            $department_info[$k]['passenger_info'] = $passenger_info;
        }
        $info = array(
            'car_use_name' => $car_passenger_info['name'],
            'department_info' => $department_info
        );

        return $this->rtn_data(1,$info);

    }

    //公务用车

    //用车类型 1：行政执法，2：特殊公务，3：重要会议，4：机要通讯，5：视察调研，6：公务接待
    public function use_car_type(){
        $info = config('base.use_types');
        if($info){
            return $this->rtn_data(1,$info);
        }else{
            return $this->msg_rtn(0,'暂无用车类型！');
        }
    }

    //车辆类型，2：优选型，3：舒适型，4：豪华型，5：小巴，6：中巴，7：大巴，8：执法车
    public function car_type(){
        $info = config('base.car_types');
        if($info){
            return $this->rtn_data(1,$info);
        }else{
            return $this->msg_rtn(0,'暂无车辆类型！');
        }
    }


    //公共 个人  / 公务 用车费用计算
    public function use_car_daily(){

        $p = input('post.');

        if(request()->isPost()){

            //区域信息
            $area_where['area_name'] = $p['area_name'];
            $area_where['state'] = '1';
            $area_info = Db::name('area')->where($area_where)->find();

            //车辆类型信息
            $car_type_where['area_id'] = $area_info['area_id'];

            //类型。2：约组车，3：公务车，4：私包车，5：公包车
            $car_type_where['type'] = $p['type'];
            //车辆类型，2：优选型，3：舒适型，4：豪华型，5：小巴，6：中巴，7：大巴，8：执法车
            $car_type_where['car_type'] = $p['car_type'];
            $car_type_where['state'] = '1';   //启用   2-禁用
            $car_info = Db::name('car_type')->where($car_type_where)->find();

            if($area_info){

                if($car_info){

                    //调用计算
                    $cost_info = $this->car_daily($p,$car_info);
                    if($cost_info){

                        return $this->rtn_data(1,$cost_info);

                    }else{
                        return $this->msg_rtn(0,'计算车费出错！');
                    }

                }else{

                    return $this->msg_rtn(0,'无该车辆类型/已停用！');
                }

            }else{

                return $this->msg_rtn(0,'该区域暂未开放！');
            }
        }else{
            return $this->msg_rtn(0,'提交方式错误！');
        }
    }

    // 里程算出车费
    public function car_daily($p,$car_info){

        //类型。2：约组车，3：公务车，4：私包车，5：公包车
        if($p['type'] == 2 || $p['type'] == 3){

            //总费用 = 额外+基础
            $all_cost = $this->milesFee($car_info,$p['estimate_mileage']);

            //订单类型,1：公务订单，2：企业订单，3：个人订单
            //   是否需要返程  1：需要  9：无需
            if($p['order_type'] == '1'){

                if($p['is_return'] == '1'){
                    $all_cost = $all_cost + $all_cost * config('passenger.car_cost')['order_back_discount'];
                }
            }

            $cost_info['starting_price'] = $car_info['starting_price'];  //起步价
            $cost_info['estimate_cost'] = $all_cost;  //预计费用

        }elseif($p['type'] == 4 || $p['type'] == 5){


        }

        return $cost_info;
    }

    //申请用车--区域内审批员列表
    public function change_part_approval(){
        $p = input('post.');
        $where['u.area_id'] = Db::name('area')->where('area_name',$p['area_name'])->value('area_id');
        $where['u.role_id'] = '3';
        $where['u.state'] = '1';

        $list = Db::name('user')->alias('u')
            ->join('passenger pr','u.user_id = pr.user_id')
            ->field('pr.name AS approval_name,pr.phone AS approval_phone')
            ->where($where)->whereNull('pr.delete_time')
            ->select();
        if($list){
            return $this->rtn_data(1,$list);
        }else{
            return $this->msg_rtn(0,'暂无审批员！');
        }
    }

    //提交订单申请
    public function order_apply(){

        if(request()->isPost()){
            $data = $this->person_ordata();
            $resl = Db::name('order')->insert($data);

            if($resl){

                $info['order_no'] = $data['order_no'];
                return $this->rtn_data(1,$info);
            }else{

                return $this->msg_rtn(0,'系统异常，下单失败！');
            }
        }
    }

    //获取订单参数
    public function person_ordata(){
        $p = input('post.');

        $ar_where['area_name'] = $p['area_name'];
        $ar_where['state'] = '1';
        $area_id = Db::name('area')->where($ar_where)->value('area_id');

        if($area_id){

            $where['area_id'] = $area_id;
            //类型。2：约组车，3：公务车，4：私包车，5：公包车
            $where['type'] = $p['type'];
            //车辆类型，2：优选型，3：舒适型，4：豪华型，5：小巴，6：中巴，7：大巴，8：执法车
            $where['car_type'] = $p['car_type'];
            $where['state'] = '1';
            $car_type_id = Db::name('car_type')->where($where)->value('car_type_id');

            if($car_type_id){

                //订单类型 订单类型,1：公务订单，2：企业订单，3：个人订单
                $order_type = $p['order_type'];

                $sj = $this->sj_rand();
                $order_no = strtotime(date('Y-m-d H:i:s',time()))."$sj";    //订单号
                $data = [
                    'car_type_id' => $car_type_id,
                    'order_no' => $order_no,
                    'passenger_name' => $p['passenger_name'],
                    'passenger_phone' => $p['passenger_phone'],
                    'order_state' => '1',
                    'order_type' => $order_type,
                    'order_time' => date('Y-m-d H:i:s',time()), //下单时间
                    'orderer' => $p['orderer'],    //下单人
                    'departure' => $p['departure'],       //出发地
                    'departure_coord' => $p['departure_coord'],       //出发地经纬度
                    'estimate_mileage' => $p['estimate_mileage'],  //预计里程数
                    'apply_time' => date('Y-m-d H:i:s',time()),  //申请时间
                    'booking_type'=> $p['booking_type'], //订车类型：1，订车（日常）。2，包车。3，分时租赁
                ];

//                //是否是有预约时间字段
//                if($p['appointment_time']){
//                    $data['appointment_time'] = $p['appointment_time'];
//                }

                //订单类型,1：公务订单，2：企业订单，3：个人订单
                //   是否需要返程  1：需要  9：无需
                if($order_type == 1){

                    $data['passenger_id'] = $p['passenger_id'];   //乘车人ID
                    $data['is_return'] = $p['is_return'];       //1-需要返程  9-无需
                    $data['approval'] = $p['approval_name'];    //审批人
                    $data['use_explain'] = $p['use_explain'];  //用车说明
                    $data['use_type'] = $p['use_type'];        //用车类型 1：行政执法，2：特殊公务，3：重要会议，4：机要通讯，5：视察调研，6：公务接待

                    //公务 日常=1  包车=2
                    if($p['booking_type'] == '1'){

                        $data['passenger_id'] = $p['passenger_id'];            //乘车人ID
                        $data['destination'] = $p['destination'];              //目的地
                        $data['destination_coord'] = $p['destination_coord'];   //目的地经纬度

                    }elseif ($p['booking_type'] == '2'){


                    }
                }elseif($order_type == 3){

                    $data['destination'] = $p['destination'];              //目的地
                    $data['destination_coord'] = $p['destination_coord'];   //目的地经纬度
                }
                return $data;

            }else{

                return $this->msg_rtn(0,'该车型暂未开放！');
            }

        }else{

            return $this->msg_rtn(0,'该地区暂未开放！');
        }

    }

    //拥有审批权限的乘客---待审批、审批历史
    public function approval_auth_person(){
        $p = input('post.');
        $where['or.approval'] = $p['approval_name'];
        //订单类型,1：公务订单，2：企业订单，3：个人订单
        $where['or.order_type'] = $p['order_type'];
        //订单状态，1：待审批，2：已批准，3：被驳回，
        $where['or.order_state'] = $p['order_state'];

        $role_id = $p['role_id'];
        if($role_id == '3'){

            $info = Db::name('order')->alias('or')
                ->join('passenger pr','or.passenger_id = pr.passenger_id')
                ->join('car_type ct','or.car_type_id = ct.car_type_id')
                ->field('pr.name AS passenger_name,pr.phone AS passenger_phone,or.car_type_id,or.order_no,or.order_time,or.departure,
                or.destination,or.estimate_mileage,or.order_type,or.is_return,or.use_type,or.use_explain,ct.car_type')
                ->where($where)
                ->select();

            foreach ($info as $k=>$val){

                $car_type_where['car_type_id'] = $val['car_type_id'];
                $car_type_info = Db::name('car_type')->where($car_type_where)->find();

                if($car_type_info){

                    $p['type'] = $car_type_info['type'];
                    $p['estimate_mileage'] = $val['estimate_mileage'];
                    $p['order_type'] = $val['order_type'];
                    $p['is_return'] = $val['is_return'];

                    //调用计算
                    //总费用 = 额外+基础
                    $cost_info = $this->car_daily($p,$car_type_info);
                    if($cost_info){

                        $info[$k]['estimate_cost'] = $cost_info['estimate_cost'];  //预估费用

                    }else{
                        return $this->msg_rtn(0,'计算车费出错！');
                    }
                }else{

                    return $this->msg_rtn(0,'无该类型信息！');
                }

            }

            if($info){
                return $this->rtn_data(1,$info);
            }else{
                return $this->msg_rtn(0,'暂无审批订单！');
            }

        }else{

            $this->msg_rtn($code=0,$msg='权限不足！');
        }
    }

    //进行审批操作
    public function approval_opration(){
        $p = input('post.');
        $where['order_no'] = $p['order_no'];

        //2-批准 3-驳回
        if($p['order_state'] == '2'){
            $data['order_state'] = '2';

        }elseif ($p['order_state'] == '3'){

            $data['order_state'] = '3';
        }

        $data['approval_time'] = date('Y-m-d H:i:s',time());
        $resl = Db::name('order')->where($where)->update($data);

        if($resl){
            return $this->msg_rtn(1);
        }else{
            return $this->msg_rtn(0,'审批失败！');
        }
    }

    //乘客点击用车
    public function passenger_use_car(){
        $p = input('post.');
        $where['order_no'] = $p['order_no'];
        //2-已批准
        $where['order_state'] = '2';
        //出发地经纬度
        $departure_coord = Db::name('order')->where($where)->value('departure_coord');
        $coord_info = explode(',',$departure_coord);

        $start_lat = $coord_info[0];
        $start_lng = $coord_info[1];
        $square = $this->returnSquarePoint($start_lat,$start_lng);

        //车辆 longitude 经度  latitude 纬度
        $car_where['latitude'] = array('latitude','between',[$square['right-bottom']['lat'],$square['left_top']['lat']]);
        $car_where['longitude'] = array('longitude','between',[$square['right-bottom']['lng'],$square['left_top']['lng']]);
        $car_where['driver_state'] = '3';

        $info = Db::name('car')->where($car_where)->whereNull('delete_time')->field('car_id,latitude,longitude')->select();

        if($info){

            $short_car = $info[0];
            $short_distance = $this->GetDistance($start_lat, $start_lng, $short_car['latitude'], $short_car['longitude']);
            foreach ($info as $k => $val) {
                $distance = $this->GetDistance($start_lat, $start_lng, $val['latitude'], $val['longitude']);

                if ($distance < $short_distance){
                    $short_key = $info[$k];
                    $short_distance = $distance;
                }
            }

            $car_id = $info[$short_key]['car_id'];
            $data['car_id'] = $car_id;
            $data['order_state'] = '5';  //已派车
            $resl = Db::name('order')->where('order_no',$p['order_no'])->update($data);

            if($resl){
                $info['car_id'] = $car_id;
                return $this->msg_rtn(1,$info);
            }else{
                return $this->msg_rtn(0,'系统异常，用车失败！');
            }

        }else{
            return $this->msg_rtn(0,'附近暂无可用车辆！');
        }
    }

    //获取以原点为圆心 -- 3公里为半径 划一个圆
    public function returnSquarePoint($lng, $lat,$distance = 3)
    {
        $earthRadius = 6378138;
        $dlng =  2 * asin(sin($distance / (2 * $earthRadius)) / cos(deg2rad($lat)));
        $dlng = rad2deg($dlng);
        $dlat = $distance/$earthRadius;
        $dlat = rad2deg($dlat);
        return $square = array(
            'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
            'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
            'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
            'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
        );
    }

    //获取2点之间的距离
    function GetDistance($lat1, $lng1, $lat2, $lng2){
        $pi = 3.1415926535898;
        $radius = 6378.137;

        $radLat1 = $lat1 * ($pi / 180);
        $radLat2 = $lat2 * ($pi / 180);
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * ($pi / 180)) - ($lng2 * ($pi / 180));
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
        $s = $s * $radius;
        $s = round($s * 10000) / 10000;
        return $s;
    }

    //获取用车成功后订单信息
    public function get_use_car(){
        $p = input('post.');
        $info = Db::name('order')->alias('or')
            ->join('car_brand cd','or.car_id = cd.car_id')
            ->join('car cr','or.car_id = cr.car_id')
            ->join('driver_car dc','or.car_id = dc.car_id')
            ->join('driver dr','dc.driver_id = dr.driver_id')
            ->field('or.car_id,or.order_time,or.departure,or.destination,dr.head_img AS driver_head_img,dr.name AS driver_name,dr.phone AS driver_phone,
            cd.name AS car_brand_name,cr.number AS car_number,cr.colour')
            ->where('order_no',$p['order_no'])
            ->find();
        $car_where['car_id'] = $info['car_id'];
        $car_where['order_state'] = '11';
        $info['avg_star'] = Db::name('order')->where($car_where)->avg('start');
        $info['order_number'] = Db::name('order')->where($car_where)->count();

        if($info){
            return $this->rtn_data(1,$info);
        }else{
            return $this->msg_rtn(0,'系统异常！');
        }
    }

    //到达后支付
    public function order_pay(){
        $p = input('post.');
        $where['order_no'] = $p['order_no'];
        $order_state = Db::name('order')->where($where)->value('order_state');
        if(request()->isPost()){

            if($order_state == '8'){

                $this->makeWeChatPay('订单支付','鹏程出行',$p['order_no'],$p['money']);

            }else{
                $this->msg_rtn(0,'该订单尚未完成！');
            }

        }else{
            $this->msg_rtn(0,'请求方式错误！');
        }
    }

    //支付后评价-评星
    public function evaluation_star(){
        $p = input('post.');
        $data['evaluation'] = $p['evaluation'];
        $data['star'] = $p['star'];

        $resl = Db::name('order')->where('order_no',$p['order_no'])->update($data);
        if($resl){
            return $this->msg_rtn($code=1);
        }else{
            return $this->msg_rtn($code=0,$msg='系统异常，评价出错！');
        }
    }

    //我的订单信息 --订单详情
    public function for_order(){
        $p = input('post.');
        $where['or.orderer'] = $p['orderer'];
        $info = Db::name('order')->alias('or')
            ->join('car_type ct','or.car_type_id = ct.car_type_id')
            //获取审批人
            ->join('passenger pr','or.approval = pr.name')
            ->order('order_time desc')
            ->where($where)->whereIn('or.order_state',[1,2,3,4,5,6,7,8,9,10])
            ->field('or.order_no,or.order_time,or.order_state,or.departure,or.destination,or.is_return,or.booking_type,or.order_type,
            or.use_type,ct.car_type,or.approval AS approval_name,pr.phone AS approval_phone,or.passenger_name,or.passenger_phone')
            ->select();

        //订单状态，1：待审批，2：已批准，3：已拒绝，4：待用车，5：已派车，6：接驾中，
        //7：已到达出发地，8：已到达目的地，9：待支付，10：待评价，11：已完成，12：已取消，13：已终止，14：无效
        if($info){

            return $this->rtn_data(1,$info);
        }else{
            return $this->msg_rtn(0,'无订单信息！');
        }
    }

    //乘客取消订单
    public function cancel_order(){
        $p = input('post.');
        $where['order_no'] = $p['order_no'];
        $resl = Db::name('order')->where($where)->setField('order_state',12);
        if($resl){
            return $this->msg_rtn(1);
        }else{
            return $this->msg_rtn(0,'取消失败！');
        }
    }

    //历史订单-- 公务 、 个人
    public function passenger_opration_order(){
        $p = input('post.');
        $where['or.orderer'] = $p['orderer'];
        //订单类型,1：公务订单，2：企业订单，3：个人订单
        $where['or.order_type'] = $p['order_type'];

        if($p['order_type'] == '1'){

            $info = Db::name('order')->alias('or')
                //关联车辆类型表
                ->join('car_type ct','or.car_type_id = ct.car_type_id')
                //乘车人
                ->join('passenger pr','or.approval = pr.name')
                ->where($where)->order('order_time desc')
                ->field('or.order_time,or.order_state,or.departure,or.destination,or.use_type,
                    or.booking_type,or.approval AS approval_name,pr.phone AS approval_phone,ct.car_type')
                ->select();
        }elseif($p['order_type'] == '3'){

            $info = Db::name('order')->alias('or')
                //关联车辆类型表
                ->join('car_type ct','or.car_type_id = ct.car_type_id')
                ->where($where)->order('order_time desc')
                ->field('or.order_time,or.order_state,or.departure,or.destination,
                    or.booking_type,ct.car_type')
                ->select();
        }

        if($info){

            return $this->rtn_data(1,$info);
        }else{

            return $this->msg_rtn(0,'暂无历史订单！');
        }
    }

    //乘客基本资料设置 -- 侧栏

    //上传头像
    public function upload_head_img(){
        $p = input('post.');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('head_portrait');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                $data['head_portrait'] = $info->getFilename();
                $resl = Db::name('passenger')->where('passenger_id',$p['passenger_id'])->update($data);
                if($resl){
                    return $this->msg_rtn(1);
                }else{
                    return $this->msg_rtn(0,'系统异常！上传失败!');
                }
                
            }else{
                // 上传失败获取错误信息
                 $msg = $file->getError();
                return $this->msg(0,$msg);
            }
        }else{
            return $this->msg_rtn(0,'请选择上传的图片！');
        }
    }

    //我的优惠卷 1-- 历史：2已使用，3已过期
    public function passenger_coupon(){
        $p = input('post.');

        $where['pc.passenger_id'] = $p['passenger_id'];
        $now_date = date('Y-m-d',time());

        if($p['type'] == 1){
            $where['pc.used'] = '9';  //1-使用  9-未使用
            $where['expiration_date'] = array('egt',$now_date);

        }elseif($p['type'] == 2){

            $where['pc.used'] = '1';  //1-使用  9-未使用

        }elseif($p['type'] == 3){

            $where['expiration_date'] = array('lt',$now_date);
        }

        $info = Db::name('passenger_coupon')->alias('pc')
            ->join('coupon cp','pc.coupon_id = cp.coupon_id')
            ->join('')
            ->field('')
            ->select();
    }

    //我的钱包
    public function passenger_wallet(){
        $p = input('post.');

        $info = (array)Db::name('passenger')
            ->where('passenger_id',$p['passenger_id'])->field('balance,business_quota,unit__quota,card_number,wechat')
            ->find();
        return $this->rtn_data(1,$info);

    }

    //充值
    public function wallet_recharge(){
        $p = input('post.');

        $data['passenger_id'] = $p['passenger_id'];
        $data['area_id'] = Db::name('area')->where('user_id',$p['user_id'])->value('area_id');
        $data['recharge_time'] = date('Y-m-d H:i:s',time());
        $data['money'] = $p['money'];

        $sj = $this->sj_rand();
        $recharge_no = strtotime(date('Y-m-d H:i:s',time()))."$sj";    //订单号
        $data['recharge_no'] = $recharge_no;
        $data['is_success'] = '9';

        $resl = Db::name('recharge_record')->insert($data);
        if($resl){

            $this->makeWeChatPay('充值','鹏程出行',$recharge_no,$p['money']);
        }else{

            return $this->msg_rtn(0,'系统异常，生成订单失败！');
        }
    }

    //统一下单
    private function makeWeChatPay($description, $title, $orderno, $money)
    {
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $description =mb_substr($description,0,20,'utf-8');
        $title =mb_substr($title,0,10,'utf-8');
        $input->SetBody($description);
        $input->SetAttach("test");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($money);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($title);
        $input->SetNotify_url("http://new.jjcxfw.com/api/Wxrtn/wxnotify");
        $input->SetTrade_type("APP");
        $UnifiedOrderResult = \WxPayApi::unifiedOrder($input);

        //二次加签
        if(!array_key_exists("appid", $UnifiedOrderResult)
            || !array_key_exists("prepay_id", $UnifiedOrderResult)
            || $UnifiedOrderResult['prepay_id'] == "")
        {
            //var_dump($UnifiedOrderResult);
            $this->msg_rtn(0,'微信统一下单失败！');
        }else{

            $params = array(
                'appid' => $UnifiedOrderResult['appid'],
                'partnerid' => $UnifiedOrderResult['mch_id'],
                'prepayid' => $UnifiedOrderResult['prepay_id'],
                'package' => 'Sign=WXPay',
                'noncestr' => \WxPayApi::getNonceStr(),
                'timestamp' => time(),
            );

            // 拼装数据进行签名
            $getsin = new \WxPaySign();
            $params['sign'] = $getsin->SetSign($params);   // 获取签名

            if($params['sign']){

                return $this->rtn_data($code =1,$params);

            }else{
                return $this->msg_rtn(0,'缺少sign值！');
            }
        }
    }

    //提现
    public function wallet_withdraw(){
        $p = input('post.');
    }

    //交易记录
    public function trading_detail(){
        $p = input('post.');
    }

    //设置提现密码
    public function pay_pwd(){
        $p = input('post.');
        $where['user_id'] = $p['user_id'];

        $user_info = Db::name('user')->where($where)->field('is_set_pwd,withdraw_pwd')->find();

        $is_set = $user_info['is_set_pwd'];
        $new_pay_pwd = md5($p['new_pay_pwd']);
        //9-未设置 1-设置
        if($is_set == '9'){

            $data['withdraw_pwd'] = $new_pay_pwd;
            $resl = Db::name('user')->where($where)->update($data);

        }else{

            if($user_info['withdraw_pwd'] == md5($p['old_pay_pwd'])){

                $resl = Db::name('user')->where($where)->setField('withdraw_pwd',$new_pay_pwd);

            }else{
                return $this->msg_rtn(0,'旧密码错误！');
            }
        }

        if($resl){
            return $this->msg_rtn(1);
        }else{
            return $this->msg_rtn(0,'系统异常，修改失败！');
        }
    }





}